package com.cskaoyan.market.admin.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.admin.service.AdminGoodsService;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsAttribute;
import com.cskaoyan.market.db.domain.MarketGoodsProduct;
import com.cskaoyan.market.db.domain.MarketGoodsSpecification;
import com.cskaoyan.market.db.mapper.MarketGoodsAttributeMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsProductMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsSpecificationMapper;
import com.cskaoyan.market.util.MyBatisUtil;

/**
 * @author BlackFlame33
 * @date 2023/03/21
 **/
public class AdminGoodsServiceImpl implements AdminGoodsService {
    @Override
    public int addGoods(MarketGoods goods, MarketGoodsSpecification[] specifications, MarketGoodsProduct[] products,
        MarketGoodsAttribute[] attributes) {
        // 将goods的数据存储到商品表时，需要获取所有规格的最低价格，存储在retailPrice字段中
        BigDecimal retailPrice = new BigDecimal(Integer.MAX_VALUE);
        for (MarketGoodsProduct product : products) {
            // 遍历每一个规格的价格，找到最低的价格，赋值给retailPrice，进而存储到goods表中
            if (retailPrice.compareTo(product.getPrice()) > 0) {
                // retailPrice大于product.getPrice()
                retailPrice = product.getPrice();
            }
        }
        // 设置最小的价格，吸引他人点击你的商品
        goods.setRetailPrice(retailPrice);
        goods.setAddTime(LocalDateTime.now());
        goods.setUpdateTime(LocalDateTime.now());
        goods.setDeleted(false);
        SqlSession sqlSession = MyBatisUtil.getSession();
        MarketGoodsMapper goodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
        MarketGoodsSpecificationMapper specificationMapper = sqlSession.getMapper(MarketGoodsSpecificationMapper.class);
        MarketGoodsProductMapper productMapper = sqlSession.getMapper(MarketGoodsProductMapper.class);
        MarketGoodsAttributeMapper attributeMapper = sqlSession.getMapper(MarketGoodsAttributeMapper.class);
        try {
            // 当我们执行完插入之后，我们再执行goods.getId便可以获取到商品的id
            goodsMapper.insertSelective(goods);
            Integer goodsId = goods.getId();
            // 插入规格表，规格需要关联商品的编号
            for (MarketGoodsSpecification sp : specifications) {
                sp.setGoodsId(goodsId);
                sp.setAddTime(LocalDateTime.now());
                sp.setUpdateTime(LocalDateTime.now());
                sp.setDeleted(false);
                specificationMapper.insertSelective(sp);
            }
            for (MarketGoodsProduct prod : products) {
                prod.setGoodsId(goodsId);
                prod.setUpdateTime(LocalDateTime.now());
                prod.setAddTime(LocalDateTime.now());
                prod.setDeleted(false);
                productMapper.insertSelective(prod);
            }
            for (MarketGoodsAttribute att : attributes) {
                att.setGoodsId(goodsId);
                att.setAddTime(LocalDateTime.now());
                att.setUpdateTime(LocalDateTime.now());
                att.setDeleted(false);
                attributeMapper.insertSelective(att);
            }
            sqlSession.commit();
        } catch (Exception e) {
            sqlSession.rollback();
            return 500;
        } finally {
            sqlSession.close();
        }
        return 200;
    }
}